import axios from "axios";
axios.defaults.withCredentials = true;

export default function(url,getOrPost,data,func){
    const token = localStorage.getItem('token');
    if(getOrPost === 'get'){
        if(data != null){
            axios({
                url:url,
                method:'get',
                data,
                headers:{
                    Authorization: token
                }
            }).then(
                response => {
                    if(response.data.result === 'success'){
                        func(response.data.data);
                    }else{
                        console.log(response.data.msg);
                        return;
                    }
                },
                error => {
                    if(error.response.status === 701){
                        window.location.replace('http://localhost:8080/');
                    }else{
                        console.log(error.message);
                    }
                }
            )
        }else{
            axios({
                url:url,
                method:'get',
                headers:{
                    Authorization: token
                }
            }).then(
                response => {
                    if(response.data.result === 'success'){
                        func(response.data.data);
                    }else{
                        console.log(response.data.msg);
                        return;
                    }
                },
                error => {
                    if(error.response.status === 701){
                        window.location.replace('http://localhost:8080/');
                    }else{
                        console.log(error.message);
                    }
                }
            )
        }
    }else{
        if(data != null){
            axios({
                url:url,
                method:'post',
                data,
                headers:{
                    Authorization: token
                }
            }).then(
                response => {
                    if(response.data.result === 'success'){
                        func(response.data.data);
                    }else{
                        console.log(response.data.msg);
                        return;
                    }
                },
                error => {
                    if(error.response.status === 701){
                        window.location.replace('http://localhost:8080/');
                    }else{
                        console.log(error.message);
                    }
                }
            )
        }else{
            axios({
                url:url,
                method:'post',
                headers:{
                    Authorization: token
                }
            }).then(
                response => {
                    if(response.data.result === 'success'){
                        func(response.data.data);
                    }else{
                        console.log(response.data.msg);
                        return;
                    }
                },
                error => {
                    if(error.response.status === 701){
                        window.location.replace('http://localhost:8080/');
                    }else{
                        console.log(error.message);
                    }
                }
            )
        }
    }
}